﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ImageAppDataModels;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using ImageAppViewModels;
using System;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FileNameFilterControl.xaml
    /// </summary>
    public partial class FileNameFilterControl : UserControl
    {
        public event EventHandler RequestDone;

        public static readonly DependencyProperty SettingsViewModelProperty =
            DependencyProperty.Register("SettingsViewModel", typeof(SettingsViewModel), typeof(FileNameFilterControl), new PropertyMetadata(null, OnSettingsViewModelPropertyChanged));

        public SettingsViewModel SettingsViewModel
        {
            get { return (SettingsViewModel)GetValue(SettingsViewModelProperty); }
            set { SetValue(SettingsViewModelProperty, value); }
        }

        private static void OnSettingsViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FileNameFilterControl source = d as FileNameFilterControl;
            SettingsViewModel vNew = e.NewValue as SettingsViewModel;
            SettingsViewModel vOld = e.OldValue as SettingsViewModel;
            source.UpdateSettingsViewModel(vNew, vOld);
        }

        private void UpdateSettingsViewModel(SettingsViewModel viewModel, SettingsViewModel viewModelOld)
        {
            SettingsViewModel = viewModel;

            // Let the UI bind to the view-model.
            this.DataContext = SettingsViewModel;
        }

        public dynamic SettingsViewModelDynamic
        {
            get
            {
                dynamic d = SettingsViewModel;
                return d;
            }
        }

        public FileNameFilterControl()
        {
            InitializeComponent();
        }

        private void txtFileNameFilter_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                SettingsViewModelDynamic.FileNameFilter = string.Empty;
                //SettingsViewModel.FileNameFilter = string.Empty;
                e.Handled = true;
                RequestDone.RaiseEvent(this);
            }
            else if (e.Key == System.Windows.Input.Key.Enter)
            {
                SettingsViewModelDynamic.FileNameFilter = txtFileNameFilter.Text;
                // SettingsViewModel.FileNameFilter = txtFileNameFilter.Text;
                e.Handled = true;
                RequestDone.RaiseEvent(this);
            }
        }

        private void txtMinSize_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }

        private void txtMaxSize_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {

        }
    }
}
